#/usr/bin/env python
# -*- coding:utf-8 -*-

def log(message, values):
    if not values:
        print(message)
    else:
        values_str = '，'.join(str(x) for x in values)
        print('%s: %s' % (message, values_str))


# 1
print('====1====')
log('My numbers are', [1, 2])
log('Hi there', [])


def log(message, *values): # The only difference
    if not values:
        print(message)
    else:
        values_str = '，'.join(str(x) for x in values)
        print('%s: %s' % (message, values_str))


# 2
print('====2====')
log('My numbers are', 1, 2)
log('Hi there')

favorites = [7, 33, 99]
log('Favorite colors', *favorites)


# --- 问题一：变长参数会转换为tuple。当*操作的生成器为参数，容易内存耗尽


def my_generator():
    for i in range(10):
        yield i

def my_func(*args):
    print(args)


# 3
print('====3====问题一')
it = my_generator()
my_func(*it)


# --- 问题二：以后给函数添加新的位置参数（形参），就必须修改原来调用该函数的那些旧代码

def log(sequence, message, *values):
    if not values:
        print('%s: %s' % (sequence, message))
    else:
        values_str = '，'.join(str(x) for x in values)
        print('%s: %s：%s' % (sequence, message, values_str))

print('====4====问题二')
log(1, 'Favorites', 7, 33) # New usage is ok
log('Favorites', 7, 33) # Old usage break, 不报错，不好定位
